package nonDefaultPackage;

import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import com.mysql.jdbc.CallableStatement;

public class DatabaseManager {

	 static String server = "localhost";
	 static String password = "malibu";
	 static String account = "root";
	 static String database = "mysql";
	 private  java.sql.Connection con;
	
	
	/**
	 * konstruqtori private aris rom singleton klasi amovides
	 */
	private DatabaseManager(){
		 try {		
			 Class.forName("com.mysql.jdbc.Driver");
			 con = DriverManager.getConnection("jdbc:mysql://" + server, account, password);
			 java.sql.Statement stmt = con.createStatement();
			 stmt.executeQuery("USE " + database);			
			 
			 }
		 
		//es eqseption rom ar moxdes conector jar chaagdet web-inf_is lib foldershic
			  catch (ClassNotFoundException e) {
			 // TODO Auto-generated catch block
				  e.printStackTrace();
			 } catch (SQLException e) {
			 // TODO Auto-generated catch block
			 e.printStackTrace();
			 }
	}
	//sincronised
	public static DatabaseManager GetInstance(){
		if(data==null)data=new DatabaseManager();		
		return data;
	}
	
	
	/**
	 * returns iterator of all player teams from dataBase
	 * @param ch
	 * @return
	 * @throws SQLException 
	 */
	public Iterator getPlayerTeams(Championship ch) throws SQLException{
	
		ArrayList<Team>list= new ArrayList<Team>();
		 java.sql.Statement stmt = con.createStatement();
		 stmt.executeQuery("USE " + database);			
		 ResultSet rs = stmt.executeQuery("Select team_id from championshipTable");	
		while(rs.next()){
			String teamID=rs.getString(1);
			//aq unda sheiqmnas team obieqti da sheivsos bazidan
			Team temp=new Team("unda gadaeces saxeli romelic dabrundeba bazidan");
			list.add(temp);
		}
		return list.iterator();		
	}
	
	public Iterator getTeamPlayers(Team t,Championship ch) throws SQLException{
		
		ArrayList<String>list= new ArrayList<String>();
		 java.sql.Statement stmt = con.createStatement();
		 stmt.executeQuery("USE " + database);
		 int teamId=t.getId();
		 int championshipId=ch.getId();
		 ResultSet rs = stmt.executeQuery("Select player_id from teamPlayers where team_id="+teamId+" and championship_id=1;");	
		while(rs.next()){
			String playerID=rs.getString(1);
			int playerId= Integer.parseInt(playerID);			
			ResultSet rs2 = stmt.executeQuery("Select player_name, date_of_birth from players where player_id=1;");	
			System.out.println(playerId+" ai esaa rs");
			String playerName=rs2.getString(1);
		//	String playerName=rs2.getString("player_name");
			Date dabadebisTarigi=Date.valueOf(rs2.getString(2));
			Player p= new Player(playerName, dabadebisTarigi);
			// es faqtiurad dawerilia da listshi player obieqti unda chavardes
			list.add(playerName);
		}
		return list.iterator();		
	}
	
	

	private static DatabaseManager data;
}
